#!/usr/bin/perl
use strict;
package csop;

sub csop
{
    my ($dbh,$remote,$input,$csnum) = @_;
    my $level;
    my $sth = $dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
    $sth->execute;
    my $isauth = $sth->fetchrow_array;
    if ($isauth) {
	$sth = $dbh->prepare("SELECT level FROM users WHERE channel = '$$input[4]' AND rnick = '$isauth';");
	$sth->execute;
	$level = $sth->fetchrow_array;
    }
    if ($$input[5] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :OP <#channel> <nickname>\n";
    }
    elsif (!$isauth) {
	print $remote "$csnum O $$input[0] :You have yet to be identified with Chan Serv.\n";
    }
    elsif ($level < 150) {
	print $remote "$csnum O $$input[0] :Access denied.\n";
    }
    else {
	$sth=$dbh->prepare("SELECT num FROM ninfo WHERE nick = '$$input[5]';");
	$sth->execute;
	my $num = $sth->fetchrow_array;
	$sth=$dbh->prepare("SELECT num FROM onchan WHERE num = '$num' AND isop = '0' AND code = '".encode::encode($num)."';");
	$sth->execute; 
	my $true = $sth->fetchrow_array;
	if ($true) {
	    print $remote "$csnum M $$input[4] +o $num\n";
	    $sth=$dbh->prepare("UPDATE onchan SET isop = '1' WHERE num = '$num' AND code = '".encode::encode($num)."' AND channel = '$$input[4]';"); 
	    $sth->execute;
	    print $remote "$csnum O $$input[0] :Opped $$input[5] on $$input[4].\n";
	}
	else {
	    print $remote "$csnum O $$input[0] :$$input[5] must be on $$input[4] and deopped.\n";
	}
    }



    undef $sth;
}
1;
